var app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      observer: 'updateList',
    },
    cate_id: {
      type: String,
      observer: 'updateId',
    },
    flxShow:{
      type:null
    },
    goods_sort:{
      type:1
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

    choose_index: 1,
    category_index: 0,
    sort: {
      up: 0,
      down: 0
    },

    id: "",
    search_type:0,
    order_type:0,

    goods_list: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    updateId() {
      const {
        cate_id
      } = this.data;
      if (cate_id) {
        this.setData({
          id: cate_id
        })
      }
    },
    updateList() {
      const {
        list
      } = this.data;
      if (list && list.length !== 0) {
        this.setData({
          goods_list: list
        })
      }
    },
    choose(e) {
      let index = e.currentTarget.dataset.index;
      this.setData({
        choose_index: index
      })

      if (index !== 1) {
        if (this.data.sort.up == 0 && this.data.sort.down == 0) {
          this.setData({
            "sort.up": 1,
            "sort.down": 0
          })
        } else if (this.data.sort.up == 1 && this.data.sort.down == 0) {
          this.setData({
            "sort.up": 0,
            "sort.down": 1
          })
        } else if (this.data.sort.up == 0 && this.data.sort.down == 1) {
          this.setData({
            "sort.up": 1,
            "sort.down": 0
          })
        }
      } else if (index == 1) {
        this.setData({
          "sort.up": 0,
          "sort.down": 0
        })
      }
      if (this.data.sort.down == 1) {
        this.setData({
          order_type: 1
        })
      } else {
        this.setData({
          order_type: 0
        })

      }
      let search_type = index - 1;
      this.setData({
        search_type
      })
      let json ={
        page: 1,
        order_type: this.data.order_type,
        search_type,
        cate_id: this.data.id
      }
      this.getData(json)
    },
    getData(json) {
      app._getNetWork({
        url: app.getNetAddresss("plugin.tiktok-cps.api.goods.good-list"),
        data: json,
        success: resdata => {
          let data = resdata.data;
          if (data.result == 0) {
            return app.tips(data.msg)
          }
          this.setData({
            goods_list: data.data.products
          })
          this.triggerEvent("updateCategoryData",{...json,total_page:data.data.last_page,list:data.data.products})
        }
      })
    },
    link_detail(e) {
      let goods_id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/packageF/cps/detail/detail?goods_id=' + goods_id,
      })
    },
  }
})